CFLAGS  += -w -O0 -D_REENTRANT -g
LIBS    := -lpthread -lm -lssl -lcrypto

TARGET  := $(shell uname -s | tr '[A-Z]' '[a-z]' 2>/dev/null || echo unknown)
# AR = libtool -static
AR = ar rcs
CC = gcc
ifeq ($(TARGET), sunos)
	CFLAGS += -D_PTHREADS -D_POSIX_C_SOURCE=200112L
	LIBS   += -lsocket
else ifeq ($(TARGET), darwin)
	LDFLAGS += -pagezero_size 10000 -image_base 100000000
else ifeq ($(TARGET), linux)
	CFLAGS  += -D_POSIX_C_SOURCE=200112L -D_BSD_SOURCE -D_DEFAULT_SOURCE
	LIBS    += -ldl
	LDFLAGS += -Wl,-E
else ifeq ($(TARGET), freebsd)
	CFLAGS  += -D_DECLARE_C99_LDBL_MATH
	LDFLAGS += -Wl,-E
endif

SRC  := ae.c aprintf.c http_parser.c net.c units.c zmalloc.c \
		wrk_setproctitle.c wrk_channel.c ssl.c

VER  ?= $(shell git describe --tags --always --dirty)

ODIR := ../../obj
TARGET := $(ODIR)/lib/libwrk.a

OBJ  := $(patsubst %.c, $(ODIR)/%.o, $(SRC))

CFLAGS  += -I$(ODIR)/include/luajit-2.1 -I$(ODIR)/include  -I../include


all: $(TARGET)

# clean:
# 	$(RM) -rf $(BIN) obj/*

# $(BIN): $(OBJ)
# 	@echo LINK $(BIN)
# 	ar -r $@ $<

$(OBJ): ../include/config.h Makefile  | $(ODIR)

$(ODIR):
	@mkdir -p $@

	
$(ODIR)/%.o : %.c
	@echo CC $<
	$(CC) $(CFLAGS) -std=c99 -c -o $@ $<


$(TARGET): $(OBJ)
	@echo Building libwrk.a...
	$(AR) $@ $(OBJ)

# ------------

.PHONY: all clean

.SUFFIXES:
.SUFFIXES: .c .cpp .o .lua

vpath %.c   src
vpath %.h   src
